﻿/*************************************************************************
* 计算机名：AA-FA-05
* CLR 版本：4.0.30319.42000
* 命名空间: MyInformationManage
* 类    名：TreeViewNode
* 当 前 域：HYRC
* 创 建 人：dailuyu
* 创建时间：2021/6/13 15:48:03
* 描    述：
*  V0.01 2021 初版
*
*  Copyright (c) 2021 aatsr Corporation. All rights reserved.
*  ┌──────────────────────────────────┐
*  │　此技术信息为本公司机密信息，未经本公司书面同意禁止向第三方披露．　│
*  └──────────────────────────────────┘
***************************************************************************/

using Unity;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace CustomControls.TreeView
{
    [Serializable]
    public class TreeViewNode : ObservableObject, ICloneable
    {
        #region 属性字段

        private string _ObjId;
        /// <summary>
        /// 标识
        /// </summary>
        public string ObjId
        {
            get { return _ObjId; }
            set { _ObjId = value; }
        }

        private string _FObjId;
        /// <summary>
        /// F标识
        /// </summary>
        public string FObjId
        {
            get { return _FObjId; }
            set { _FObjId = value; }
        }

        private int id;
        /// <summary>
        /// 节点ID
        /// </summary>
        public int Id
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
            }
        }

        private int parentId;
        /// <summary>
        /// 父节点ID
        /// </summary>
        public int ParentId
        {
            get
            {
                return parentId;
            }
            set
            {
                parentId = value;
            }
        }

        private string nodeName;
        /// <summary>
        /// 节点名称（最多六个字符）
        /// </summary>
        public string NodeName
        {
            get
            {
                return nodeName;
            }
            set
            {
                nodeName = value;
                if (nodeName.Length > 30)
                {
                    nodeName = nodeName.Substring(0, 30);
                }
                RaisePropertyChanged("NodeName");
            }
        }

        private bool isChildNode;
        /// <summary>
        /// 是否是子节点
        /// </summary>
        public bool IsChildNode
        {
            get
            {
                return isChildNode;
            }
            set
            {
                isChildNode = value;
            }
        }

        private bool isAllowClick;

        public bool IsAllowClick
        {
            get { return isAllowClick; }
            set { isAllowClick = value; }
        }


        private BitmapImage _IconUrl;
        /// <summary>
        /// 图标
        /// </summary>
        public BitmapImage IconUrl
        {
            get { return _IconUrl; }
            set 
            {
                _IconUrl = value;
                RaisePropertyChanged("IconUrl");
            }
        }

        /// <summary>
        /// 选择图标
        /// </summary>
        public BitmapImage SelectIconUrl { get; set; }

        /// <summary>
        /// 是否改变
        /// </summary>
        public bool IsChange { get; set; }

       

        private ObservableCollection<TreeViewNode> childNodes;
        /// <summary>
        /// 子节点数据
        /// </summary>
        public ObservableCollection<TreeViewNode> ChildNodes
        {
            get
            {
                if (childNodes == null)
                {
                    childNodes = new ObservableCollection<TreeViewNode>();
                    childNodes.CollectionChanged += new NotifyCollectionChangedEventHandler(OnMoreStuffChanged);
                }
                return childNodes;
            }
            set
            {
                childNodes = value;
            }
        }
        private void OnMoreStuffChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                TreeViewNode stuff = (TreeViewNode)e.NewItems[0];
                stuff.ParentId = this.Id;
            }
            else if (e.Action == NotifyCollectionChangedAction.Remove)
            {
                TreeViewNode stuff = (TreeViewNode)e.OldItems[0];
                if (stuff.ParentId == this.Id)
                {
                    stuff.ParentId = 0;
                }
            }
        }



        #region 界面展示相关属性

        //(Color)ColorConverter.ConvertFromString("#74787c")
        private Brush _RectangleBd = new SolidColorBrush(Colors.Transparent);

        public Brush RectangleBd
        {
            get { return _RectangleBd; }
            set
            {
                _RectangleBd = value;
                RaisePropertyChanged("RectangleBd");
            }
        }

        private Brush _ShowTextBlockForeGroud = Application.Current.FindResource("ForegroundGloabal") as SolidColorBrush;
        public Brush ShowTextBlockForeGroud 
        {
            get { return _ShowTextBlockForeGroud; }
            set
            { 
                _ShowTextBlockForeGroud = value;
                RaisePropertyChanged("ShowTextBlockForeGroud");
            }
        }


        //根据节点类型设置Margin
        public string Margining
        {
            get
            {
                double padLeft;
                if (this.isChildNode == true)
                {
                    padLeft = 36;
                }
                else
                {
                    padLeft = 10;
                }
                return string.Format("{0},0,0,0", padLeft);
            }
        }
        //根据节点设置分隔线
        public string ShowBorderThickness
        {
            get
            {
                if (this.isChildNode == false)
                    return string.Format("0,2,0,0");
                else
                    return string.Format("0,0,0,0");
            }
        }
        //根据子父节点设置字体大小
        public int SetFontSize
        {
            get
            {
                if (this.isChildNode == true)
                    return 16;
                else
                    return 16;
            }
        }

        //根据子父节点设置字体宽度
        public string SetFontWeight
        {
            get
            {
                if (this.isChildNode == true)
                    return "Normal";
                else
                    return "Bold";
            }
        }

        //根据子父节点设置字体颜色
        public string SetForeground
        {
            get
            {
                if (this.isChildNode == true)
                    return "#d3d9dc";
                else
                    return "#d3d9dc";
            }
        }

        //根据子父节点设置背景颜色
        public string SetBackground
        {
            get
            {
                if (this.isChildNode == true)
                    return "#05101e";
                else
                    return "#0e2744";
            }
        }
       
        //节点是否展开
        public bool SetIsExpanded
        {
            get
            {
                if (this.isChildNode != true)
                    return false;
                else
                    return true;
            }
        }

        #region ICloneable接口的实现方法
        /// <summary>
        /// 克隆对象（内存深度拷贝）
        /// </summary>
        /// <returns></returns>
        public virtual object Clone()
        {
            object retval;
            using (MemoryStream ms = new MemoryStream())
            {
                BinaryFormatter bf = new BinaryFormatter();
                //序列化成流
                bf.Serialize(ms, this);
                ms.Seek(0, SeekOrigin.Begin);
                //反序列化成对象
                retval = bf.Deserialize(ms);
                ms.Close();
            }
            return retval;
        }
        #endregion

        #endregion
        #endregion

        #region 构造函数
        public TreeViewNode()
        {

        }

        public TreeViewNode(int _id, int _parentId, bool _isChildNode, bool _isChildNodeAdd, bool _isNodeAdd, string _nodeName)
        {
            this.id = _id;
            this.parentId = _parentId;
            this.isChildNode = _isChildNode;
            this.nodeName = _nodeName;
        }
        #endregion
    }
}
